package org.cnam.mobileShopping.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class IOUtils {
	// 16 Ko
	private static final int DEFAULT_BUFFER_SIZE = 16 * 1024;

	public static void closeQuietly(InputStream inputStream) {
		if (inputStream != null) {
			try {
				inputStream.close();
			} catch (IOException e) {
			}
		}
	}

	public static void closeQuietly(OutputStream outputStream) {
		if (outputStream != null) {
			try {
				outputStream.close();
			} catch (IOException e) {
			}
		}
	}

	public static void copyAndClose(InputStream inputStream, OutputStream outputStream) throws IOException {
		copyAndClose(inputStream, outputStream, DEFAULT_BUFFER_SIZE);
	}

	public static void copyAndClose(InputStream inputStream, OutputStream outputStream, int bufferSize)
			throws IOException {
		copy(inputStream, outputStream, bufferSize);
		closeQuietly(inputStream);
		closeQuietly(outputStream);
	}

	public static void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
		copy(inputStream, outputStream, DEFAULT_BUFFER_SIZE);
	}

	public static void copy(InputStream inputStream, OutputStream outputStream, int bufferSize) throws IOException {
		if (inputStream != null && outputStream != null) {
			int read = 0;
			byte[] buffer = new byte[bufferSize];
			while ((read = inputStream.read(buffer)) >= 0) {
				outputStream.write(buffer, 0, read);
			}
		}
	}
}
